import { get, post } from '@/utils/axios'

export default {
  props: ['title', 'dialogVisible', 'formId'],
  data() {
    return {
      form: {},
      beforeRes: {},
      beforeId: null,
      isAdd: true,
      loading: true,
      removeIds: [],
      fileList: [],
      compRefs: {}
    }
  },
  methods: {
    /**
     * 通用的提示封装
     */
    submitSuccessNotify() {
      this.$notify({
        title: '提交成功',
        type: 'success',
        duration: 2500
      })
      this.handleBeforeCloseDialog()
      this.closeCurrentDialog()
    },
    addSuccessNotify() {
      this.$notify({
        title: '新增成功',
        type: 'success',
        duration: 2500
      })
      this.handleBeforeCloseDialog()
      this.closeCurrentDialog()
    },
    editSuccessNotify() {
      this.$notify({
        title: '编辑成功',
        type: 'success',
        duration: 2500
      })
      this.handleBeforeCloseDialog()
      this.closeCurrentDialog()
    },
    validFormNotify(message) {
      const h = this.$createElement
      const errorMsg = h('ul', [...message.map(item => h('li', item))])
      this.$notify({
        title: '表单有误',
        type: 'warning',
        message: errorMsg,
        duration: 0
      })
    },
    notify(title, type) {
      this.$notify({
        title,
        type,
        duration: 2500
      })
    },
    // 关闭当前弹窗
    closeCurrentDialog() {
      this.clearValid()
      this.$parent.handleCanleDialog()
    },
    async showDialog() {
      this.beforeShowDialog()
      const { formId } = this
      // form值制为空
      this.isAdd = formId.length === 0
      // 防止再次请求
      if (this.beforeId || formId) {
        if (formId === this.beforeId) {
          // 深拷贝防止多次请求相同数据
          const beforeRes = this._.cloneDeep(this.beforeRes)
          const { form } = beforeRes
          this.form = form
          this.fillFormAfter(beforeRes)
        } else if (!this.isAdd) {
          await get(this.apiUrl.get, { formId }).then(res => {
            const { form } = res.obj
            this.beforeRes = this._.cloneDeep(res.obj)
            this.beforeId = formId
            this.form = form
            this.fillFormAfter(res)
          })
        }
      }
      this.loading = false
    },
    handleSaveForm() {
      this.beforeSaveForm()
      // 表单校验
      this.$refs.formValid.validate(valid => {
        if (valid) {
          post(this.apiUrl.save, this.form, false).then(res => {
            this.handleResultOper(res)
          })
        } else {
          this.$children[0].disabled = false
          this.notify('请确认表格填写规则', 'error')
          return false
        }
      })
    },
    handleCloseDialog() {
      this.loading = true
      this.$emit('close')
    },
    handleBeforeCloseDialog() {},
    beforeSaveForm() {},
    // 提交成功后处理
    handleResultOper(result) {
      const resFlag = result.state
      this.loading = resFlag
      if (resFlag) {
        // 修改后数据变化 需重新请求
        this.beforeId = null
        this.isAdd ? this.addSuccessNotify() : this.editSuccessNotify()
      } else {
        this.$children[0].disabled = false
        const errMsg = result.obj
        errMsg instanceof Array ? this.validFormNotify(errMsg) : this.notify(errMsg, 'error')
      }
    },
    beforeShowDialog() {}
  }
}
